//
//  PYImageGalleryImageView.m
//  PictureYourself
//
//  Created by Daniel on 8/30/12.
//
//

#import "PYImageGalleryImageView.h"

@implementation PYImageGalleryImageView

@synthesize delegate=delegate_;
@synthesize is_selected=is_selected_;
@synthesize index=index_;

-(void)showSelection:(BOOL)animated
{
    if(animated)
    {
        [UIView setAnimationCurve:UIViewAnimationCurveEaseOut];
        [UIView
         animateWithDuration:0.15
         animations:^(void)
         {
             self.alpha = 0.65;
         }];
    }
    else
    {
        self.alpha = 0.65;
    }
}

-(void)hideSelection:(BOOL)animated
{
    //NSLog(@"Hiding selection for %p\n", self);
    if(animated)
    {
        NSLog(@"My alpha: %f\n", self.alpha);
        [UIView
         animateWithDuration:0.175
         animations:^(void)
         {
             self.alpha = 1.0;
         }
         completion:^(BOOL finished)
         {
             NSLog(@"Finished: %d, My alpha: %f\n", finished, self.alpha);
         }];
    }
    else
    {
        self.alpha = 1.0;
    }
}

-(void)setSelection:(BOOL)on :(BOOL)animated
{
    if(on)
    {
        [self showSelection :animated];
    }
    else
    {
        [self hideSelection :animated];
    }
    
    self.is_selected = on;
}

- (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event
{
    [self setSelection:!self.is_selected :TRUE];
    
    if(!self.delegate)
        return;
    
    if(self.is_selected)
        [self.delegate thumbnailImageViewWasSelected:self atIndex:self.index];
    else
        [self.delegate thumbnailImageViewWasDeSelected:self atIndex:self.index];
}

@end
